package com.example.elm.CartAndOrder

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.R
import com.example.elm.databinding.ConfirmOrderBinding
import androidx.fragment.app.Fragment
import com.example.elm.Public.CartGoodsInfo


class ConfirmOrderActivity : ComponentActivity() {
    private lateinit var binding: ConfirmOrderBinding
    private val gfoodList = ArrayList<CartGoodsInfo>()
    var cost = 0.0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ConfirmOrderBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val selectedProducts = intent.getSerializableExtra("selected_products") as ArrayList<CartGoodsInfo>
        if (selectedProducts != null) {
            gfoodList.addAll(selectedProducts) // 将商品列表添加到 gfoodList
        }
        for (i in gfoodList.indices) {
            cost += gfoodList[i].price
        }
        binding.cost.text = "￥${String.format("%.2f", cost)}"

        val layoutManager = LinearLayoutManager(this)
        binding.lvRestaurantAndFoodDetails.layoutManager = layoutManager
        val adapter = gfoodAdapter(gfoodList)
        binding.lvRestaurantAndFoodDetails.adapter = adapter

        adapter.notifyDataSetChanged()

        binding.orderBack.setOnClickListener {
            showGoBackPickerDialog()
        }
        binding.submit.setOnClickListener {
            //商家将会发送 送餐信息
            Toast.makeText(this, "支付成功！", Toast.LENGTH_SHORT).show()
            finish()
        }
    }

    private fun showGoBackPickerDialog() {
        val options = arrayOf("确定取消", "继续支付")
        val builder = android.app.AlertDialog.Builder(this)
        builder.setTitle("您确定要取消订单吗？")
        builder.setItems(options) { _, which ->
            when (which) {
                0 -> { // 用户选择“确定删除”
                    finish() // 调用删除操作
                }
                1 -> { // 用户选择“取消删除”
                    // 不做任何操作，对话框会自动关闭
                }
            }
        }
        builder.show()
    }

    // 将每行layout文件，代码与控件关联
    private class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val image = view.findViewById<ImageView>(R.id.kcyFoodImage)
        val store = view.findViewById<TextView>(R.id.kcyRestaurantName)
        val price = view.findViewById<TextView>(R.id.kcyPrice)
        val name = view.findViewById<TextView>(R.id.kcyFoodName)
        val info = view.findViewById<TextView>(R.id.kcySpiciness)
        val count = view.findViewById<TextView>(R.id.kcyCount)
    }

    // 适配器，填充数据
    private class gfoodAdapter(val gfoodList:ArrayList<CartGoodsInfo>): RecyclerView.Adapter<ViewHolder> () {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(
                R.layout.confirm_wupin, parent, false)
            return  ViewHolder(view)
        }
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val info = gfoodList[position]
            val imageResId = holder.itemView.context.resources.getIdentifier(
                info.image, "drawable", holder.itemView.context.packageName
            )
            if (imageResId != 0) {
                holder.image.setImageResource(imageResId)
            } else {
                holder.image.setImageResource(R.drawable.cqxm)
            }
            holder.store.text = info.store               // 餐厅名称
            holder.name.text = info.name                 // 菜品名称
            holder.info.text = "辣度：${info.info}"  // 辣度
            holder.count.text = "份数 x${info.count}"          // 份数
            holder.price.text = "¥${info.price}"          // 价格
        }
        override fun getItemCount(): Int {
            return gfoodList.size
        }
    }
}
